package com.gitee.xcfyl.simplehttpserver.servlet;

import com.gitee.xcfyl.simplehttpserver.constants.HttpConstants;
import com.gitee.xcfyl.simplehttpserver.io.HttpRequest;
import com.gitee.xcfyl.simplehttpserver.io.HttpResponse;

/**
 * 定义抽象的Servlet
 *
 * @author 西城风雨楼
 */
public abstract class HttpServlet {
    /**
     * Servlet中定义的处理Http请求和响应的方法
     *
     * @param req Http请求对象
     * @param res Http响应对象
     * @throws Exception 可能抛出的异常
     */
    public void service(HttpRequest req, HttpResponse res) throws Exception {
        if (HttpConstants.METHOD_GET.equals(req.getMethod())) {
            // 处理get请求
            doGet(req, res);
        } else if (HttpConstants.METHOD_POST.equals(req.getMethod())) {
            // 处理post请求
            doPost(req, res);
        }
    }

    /**
     * 处理Get请求，具体实现交由子类完成
     *
     * @param req 请求
     * @param res 响应
     * @throws Exception 异常
     */
    public abstract void doGet(HttpRequest req, HttpResponse res) throws Exception;

    /**
     * 处理Post请求，具体实现交由子类完成
     *
     * @param req 请求
     * @param res 响应
     * @throws Exception 异常
     */
    public abstract void doPost(HttpRequest req, HttpResponse res) throws Exception;
}
